import datetime

from dao.blog_store import Blog_store
from utils.PutObjectUtil import putObject
from utils.RandUtil import generate_uuid

# 业务（实现功能的过程）
class BlogService():
    def list(self, page):
        data = Blog_store().list(page)
        return data

    def getCountBlog(self):
        return Blog_store().getCount()

    def getById(self,id):
        return Blog_store().queryById(id)

    def getIndexComments(self):
        return Blog_store().getCommentIndex()

    def getTheme(self):
        return Blog_store().getTheme()

    def putImg(self, data):
        baseUrl = "https://taritari-1306000991.cos.ap-chengdu.myqcloud.com/"
        imgUrl = "blog/"+generate_uuid()+".jpg"
        putObject(data, imgUrl)
        return baseUrl+imgUrl

    def addBlog(self, blog):
        blog.created_time = datetime.datetime.now()
        return Blog_store().addBlog(blog)

    def deleteFoodByID(self, id):
        return Blog_store().deleteBlog(id)

    def updateByID(self,blog,id):
        blog.id = id
        return Blog_store().updateById(blog)